//
//  GroupMemberVC.swift
//  YueChat
//
//  Created by 1 on 2024/10/30.
//

import UIKit

class GroupMemberVC: BaseVC {

    var group = DMCCGroup()
    
    var data = [DMCCMember]()
    
    init(group: DMCCGroup = DMCCGroup()) {
        super.init(nibName: nil, bundle: nil)
        self.group = group
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        layout.itemSize = CGSize(width: kScreenWidth/7, height: 80)
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
        collectionView.register(UINib(nibName: "GroupDetailHeadImgCell", bundle: nil), forCellWithReuseIdentifier: "GroupDetailHeadImgCell")
        self.title = "群成员（\(group.memberCount)）"
        requestData(begin: 0)
        
        requestNetWorkData(begin: 0)
    }
  
    func requestData(begin:Int) {
        let members = gosnMember.getMemberList(group.groupId, begin, 50)

        if members.count > 0 {
            dataSource = Array(members.reversed()) + dataSource
        }
        if self.dataSource.count < group.memberCount {
            requestData(begin: self.dataSource.count)
        }
        self.collectionView.reloadData()
    }
    
    func requestNetWorkData(begin:Int) {
        Task {
//            let members = gosnMember.getMemberList(group.groupId, begin, 30)
           let members = await gosnMember.updateMemberZone(group.groupId, begin, 30)
            await MainActor.run {
                if members.count > 0 {
                    data = Array(members.reversed()) + data
                }
                if self.data.count < group.memberCount {
                    requestNetWorkData(begin: self.data.count)
                }else {
                    self.dataSource = self.data
                    self.collectionView.reloadData()
                }
            }
 
        }
    }
    
    
    
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GroupDetailHeadImgCell", for: indexPath) as! GroupDetailHeadImgCell
         
        let member = dataSource[indexPath.row] as? DMCCMember
        let user = gosnUser.getUserInfoDefault(member!.memberId)

        cell.nameLab.text = user.getAlias()
        cell.img.sd_setImage(with: URL(string: user.portrait), placeholderImage: SDImageDefault)

        let nftStr = user.getNft()
        cell.nftImg.isHidden = nftStr!.isEmpty

        return cell
    }
    
//    func isManager () -> Bool {
//        let str = self.info.getGroupJoinTypeWithGroupId()
//        return str == "admin"
//    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let member = dataSource[indexPath.row] as? DMCCMember
        let userInfo = gosnUser.getUserInfoDefault(member!.memberId)
        if userInfo.userId == gosnUser.myId() {
            self.navigationController?.pushViewController(MySetVC(), animated: true)
        }else if gosnFriend.isFriend(userInfo.userId) {
            let detailViewController = UserDetailVC(userInfo: userInfo)
            navigationController?.pushViewController(detailViewController, animated: true)
        } else {
            let addFriendViewController = AddUserVC(userInfo: userInfo)
            navigationController?.pushViewController(addFriendViewController, animated: true)
        }
    }


}
